package com.person.jc.service.security;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.service.Service;
import com.person.jc.utils.Utils;

public class TsRoleService extends Service {
	
	public static final TsRoleService me = new TsRoleService();
	
	public List<Record> getList(Map<String, Object> map) {
	    List<Object> params = new ArrayList<Object>();
	    String temp = "";
		StringBuffer sql = new StringBuffer(
				"select t.*, \n" +
				"       (select group_concat(t2.user_name)\n" + 
				"          from ts_user_role_vcl t1, ts_user t2\n" + 
				"         where t1.user_id = t2.user_id\n" + 
				"           and t1.role_id = t.role_id\n" + 
				"           and t2.status <> 3) user_list\n" + 
				"  from ts_role t\n" + 
				" where status <> 3 ");
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("status")))){
			sql.append(" and t.status = ? ");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("roleName")))){
			sql.append(" and t.role_name like ? ");
			params.add("%"+temp+"%");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("roleId")))){
			sql.append(" and t.role_id = ? ");
			params.add(temp);
		}
		sql.append(" order by t.role_name");
		return Db.find(sql.toString(), Utils.getObjParam(params));
	}
	
	
	public List<?> getUserRoleList(String userId) {
	    List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer(
				"select t.*\n" );
		if(Utils.isNotEmpty(userId)){
			sql.append(",t1.role_id flag");
		}
		sql.append("  from ts_role t\n" );
		if(Utils.isNotEmpty(userId)){
			sql.append("  left join ts_user_role_vcl t1 on t1.role_id = t.role_id\n" + 
			"                             and t1.user_id = ? ");
			params.add(userId);
		}
		sql.append(" order by t.role_id");
		return Db.find(sql.toString(), Utils.getObjParam(params));
	}
	
	
	public void insertRole(Map<String, Object> map) {
		StringBuffer sql = new StringBuffer("insert into ts_role");
		sql.append("(role_name,status) values");
		sql.append("(?,?)");
		Db.update(sql.toString(), map.get("roleName"),map.get("status"));
		super.setCRUD("角色管理", "C", "新增角色信息:"+map.get("roleName"));
	}

	
	public void updateRole(Map<String, Object> map) {
		String role_id = String.valueOf(map.get("roleId"));
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("update ts_role set ");
		sql.append(" role_name = ? ,");
		sql.append(" status = ? ");
		sql.append(" where role_id = ? ");
		params.add(map.get("roleName"));
		params.add(map.get("status"));
		params.add(role_id);
		Db.update(sql.toString(), Utils.getObjParam(params));
		super.setCRUD("角色管理", "U", "修改角色信息:"+role_id);
		
	}
	
	public void updateRoleStatus(String ids, String status) {
		String[] id = ids.split(",");
		Object[][] paras = new Object[id.length][2];
		for (int i = 0; i < id.length; i++) {
			paras[i][0] = status;
			paras[i][1] = id[i];
		}
		String sql = " update ts_role set status = ? where role_id = ?";
		Db.batch(sql, paras, id.length);
		super.setCRUD("角色管理", "U", "更改角色状态:"+ids+"("+status+")");
	}

	public int getNameCount(Map<String, Object> map) {
		String temp = "";
		StringBuffer sql = new StringBuffer("select count(1) from ts_role where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("roleCode")))){
			sql.append(" and role_code = ? ");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("roleName")))){
			sql.append(" and role_name = ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("roleId")))){
			sql.append(" and role_id <> ?");
			params.add(temp);
		}
		return Integer.parseInt(String.valueOf(Db.queryLong(sql.toString(), Utils.getObjParam(params))));
	}
	
	
}
